<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {include file="/layout/scriptload"}
    <title>微信菜单列表</title>
</head>
<body>
<div class="box">
    <section class="content-header">
        <ol class="breadcrumb">
            <li><a><i class="fa fa-dashboard"></i> 微信菜单管理</a></li>
            <li class="active">微信菜单列表</li>
        </ol>
    </section>
    <!-- /.box-header -->
    <section class="content">
    <div class="box-body">
        <div id="example1_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
            <div class="row">
                <div class="col-sm-12">
                        <div id="example1_filter" class="dataTables_length">
                            <label>搜索微信菜单：
                                <input type="search" id="keyword" class="form-control input-sm" placeholder="分类组,创建时间" aria-controls="example1">
                            </label>
                            <button type="button" id="search" class="btn margin" style="background: #18bc9c;color: #ffffff;">搜索</button>
                            <button type="button" onclick="relist()" title="刷新" class="btn bg-aqua margin"><i class="fa fa-refresh"></i></button>
                            <button type="button" onclick="toadd()" id="addNewMenu" class="btn margin"  style="background: #18bc9c;color: #ffffff;">添加菜单</button>
                            <button type="button" onclick="toclear()" id="clear_pl" class="btn bg-aqua margin" >清空公众号</button>
                            <button type="button" onclick="todel()" class="btn bg-red margin"><i class="fa  fa-trash-o"></i> 删除</button>
                        </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <table id="example1" class="table table-bordered table-striped dataTable" role="grid" aria-describedby="example1_info">
                        <thead>
                        <tr role="row">
                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 90px;">
                                反选<input id="allchecked" type="checkbox" style="margin-left: 10px">
                            </th>
                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 190px;">菜单分类组</th>
                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 290px;">绑定的公众号</th>
                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 190px;">类型</th>
                            <th class="sorting_asc" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 297px;">内容</th>
                            <th class="sorting_asc" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 297px;">创建时间</th>
                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 361px;">操作</th>
                        </tr>
                        </thead>
                        <tbody id="tb">
                       </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <!-- /.box-body -->
    </section>
</div>
</body>
<script>
    var _keyword = '';
    $(function(){
        getlist(_keyword);
        $('#search').click(function(){
            table.destroy();
            var keyword = $('#keyword').val();
            getlist(keyword);
        })
        //反选
        $('#allchecked').click(function(){
            $("[name = checkboxed]:checkbox").each(function () {
                $(this).prop("checked", !$(this).prop("checked"));
            });
        })
    })
    function getlist(_keyword)
    {
        $.ajax({
            type: 'post',
            cache: false,
            url:'{:url("admin/Wechatmenu/getMenuList")}',
            data: {keyword:_keyword},
            success: function(data) {
                if(data.code=="0")
                {
                    $("#tb tr").remove();
                    $.each(data.info,function(index,item){
                        var html='<tr role="row" class="odd" id="tr_'+item.id+'">';
                        html+='<td style="vertical-align: middle;"><input name="checkboxed" value="'+item.id+'" type="checkbox" style="margin-left: 10px"></td>';
                        html+='<td style="vertical-align: middle;">'+item.category+'</td>';
                        html+='<td style="vertical-align: middle;"><textarea class="form-control" cols="30" rows="3">'+item.ghid_listname+'</textarea></td>';
                        html+='<td style="vertical-align: middle;">'+item.type+'</td>';
                        html+='<td style="vertical-align: middle;"><textarea class="form-control" cols="50" rows="3">'+item.content+'</textarea></td>';;
                        html+='<td style="vertical-align: middle;">'+item.createtime+'</td>';
                        html+='<td style="vertical-align: middle;text-align: center">';
                        html+='<div class="btn-group">';
                        html+=' <button onclick="toedit('+item.id+')" type="button" class="btn" style="background: #18bc9c;color: #ffffff;">详情</button>';
                        html+=' <div onclick="totencent('+item.id+')" class="btn bg-aqua">绑定公众号</div>';
                        html+=' <div onclick="tosend('+item.id+')" class="btn" style="background: #18bc9c;color: #ffffff;">执行</div>';
                        html+='</div>';
                        html+='</td>';
                        html+='</tr>';
                        $("#tb").append(html);
                    });
                    table = $('#example1').DataTable({
                        "paging": true,
                        "lengthChange": false,
                        "lengthMenu": [[10],[10]],
                        "searching": false,
                        "ordering": false,
                        "info": true,
                        "language": {
                            "info": "展示第 <span style='color: red;'>_START_</span> 到 <span style='color: red;'>_END_</span> 个 共 <span style='color: red;'>_TOTAL_</span> 个菜单",
                            "paginate": {
                                "previous": "上一页",
                                "next": "下一页"
                            }
                        },
                        "autoWidth": true,
                    });
                }
            },
            error: function() {
                alert("发生系统错误,请与管理员联系！");
            }
        });
    }
    //添加菜单
    function toadd() {
        url = "{:url('wechatmenu/addMenu')}";
        layerWindow('添加微信菜单',url,'90%','80%');
    }
    //编辑菜单
    function toedit(_id) {
        url = "{:url('wechatmenu/editMenu')}" + "?mid=" + _id;
        layerWindow('编辑菜单信息',url,'80%','80%');
    }
    //设置公众号
    function totencent(_id) {
        url = "{:url('wechatmenu/setTencentGhid')}" + "?mid=" + _id;
        layerWindow('设置公众号',url,'80%','80%');
    }
    //立即执行
    function tosend(_id) {
        $.ajax({
            type: 'post',
            cache: false,
            url: '{:url("/wechat/menu.menuset/sendWechatMenu")}',
            data: {mid: _id},
            success: function (data) {
                if (data.errcode == 0) {
                    layerAlert('操作成功');
                } else {
                    layerAlert('操作失敗');
                }
            },
            error: function () {
                layerAlert('发生系统错误,请与管理员联系！');
            }
        });
    }
    //清空公众号
    function toclear(){
        var chk_value =[];
        $('input[name="checkboxed"]:checked').each(function(){
            chk_value.push($(this).val());
        });
        $.ajax({
            type: 'post',
            cache: false,
            url:'{:url("admin/Wechatmenu/doClearMenu")}',
            data: {chk_value:chk_value},
            success: function(data) {
                layerAlert('操作成功',function(){
                    table.destroy();
                    getlist(_keyword);
                })
            }
        })
    }
    //删除菜单
    function todel(){
        var chk_value =[];
        $('input[name="checkboxed"]:checked').each(function(){
            chk_value.push($(this).val());
        });
        $.ajax({
            type: 'post',
            cache: false,
            url:'{:url("admin/Wechatmenu/doDeleteMenu")}',
            data: {chk_value:chk_value},
            success: function(data) {
                layerAlert('删除成功',function(){
                    table.destroy();
                    getlist(_keyword);
                })
            }
        })
    }
    function relist()
    {
        table.destroy();
        getlist(_keyword);
    }
</script>
</html>